#!/bin/bash -ex

# Script for installing all Py versions specified as comma separated list
# in ${1} or in ${PYTHON_VERSIONS}.

if [[ ! ${H2O_BRANCH} ]]; then
 echo "H2O_BRANCH must be set"
 exit 1
fi

VERSIONS_TO_INSTALL=${1}
if [[ ! -n ${VERSIONS_TO_INSTALL} ]]; then
  VERSIONS_TO_INSTALL=${PYTHON_VERSIONS}
fi
if [[ ! -n ${VERSIONS_TO_INSTALL} ]]; then
  echo "Either PYTHON_VERSIONS must be set or provide and argument containing required versions"
  exit 1
fi

# Download requirements files from GitHub
wget -nv https://raw.githubusercontent.com/h2oai/h2o-3/${H2O_BRANCH}/h2o-py/test-requirements.txt
wget -nv https://raw.githubusercontent.com/h2oai/h2o-3/${H2O_BRANCH}/h2o-py/docs-requirements.txt

IFS=',' read -r -a array <<< ${VERSIONS_TO_INSTALL}
for python_version in "${array[@]}"; do
  echo "###### Installing Python ${python_version} ######"
  if [[ "${python_version}" =~ ^([0-9])\.([0-9]+).* ]]; then
    major=${BASH_REMATCH[1]}
    minor=${BASH_REMATCH[2]}
  else
    echo "Invalid Python version: ${python_version}"
    exit 1
  fi
  if [[ ${major} == "2" ]] ; then pkg_suffix="" ; else pkg_suffix=${major} ; fi

  apt-get update
  if [[ ${python_version} == "3.6" ]]
  then
    DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential checkinstall libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev clang
    pushd /usr/src
    wget -nv https://www.python.org/ftp/python/3.6.15/Python-3.6.15.tar.xz
    echo "bc04aa6c2a1a172a35012abd668538cd Python-3.6.15.tar.xz" > Python-3.6.15.tar.xz.md5
    md5sum -c Python-3.6.15.tar.xz.md5 || exit 1  # unexpected checksum
    tar xf Python-3.6.15.tar.xz
    cd Python-3.6.15
    export CC="clang"  # needed to avoid SegFault during install
    ./configure
    make
    make install
    popd
  else
    DEBIAN_FRONTEND=noninteractive apt-get -y install python${python_version} python${python_version}-dev python${pkg_suffix}-setuptools
    if [[ ${major} -ge 3 && ${minor} -ge 7 ]]; then
        DEBIAN_FRONTEND=noninteractive apt-get -y install python${python_version}-distutils
    fi
  fi
  apt-get clean
  rm -rf /var/cache/apt/*

  echo "###### Installing pip for Python ${python_version} ######"
  if [[ ${major} == "2" ]] || [[ ${python_version} == "3.5" ]] || [[ ${python_version} == "3.6" ]] || [[ ${python_version} == "3.7" ]]
  then
    curl https://bootstrap.pypa.io/pip/${python_version}/get-pip.py --output get-pip.py
    python${python_version} get-pip.py
  else
    curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
    python${python_version} get-pip.py
  fi

  echo "###### Creating Virtualenv for Python ${python_version} ######"
  python${python_version} -m pip install virtualenv
  mkdir -p /envs/h2o_env_python${python_version}
  chmod a+w -R /envs/h2o_env_python${python_version}
  su jenkins -c "virtualenv -p python${python_version} /envs/h2o_env_python${python_version}"

  echo "###### Installing dependencies for Python ${python_version} ######"
  su jenkins -c ". /envs/h2o_env_python${python_version}/bin/activate && pip uninstall setuptools -y && pip install setuptools" # resinstalling setuptools fixes install issues for sphinx-osexample
  # FIXME do not grep test-requirements.txt but use pip install -r test-requirements.txt to ensure proper versions
  # Currently we cannot ensure any version defined in test-requirements.txt
  su jenkins -c ". /envs/h2o_env_python${python_version}/bin/activate && grep -v \"^#\" test-requirements.txt | sed -r 's/(.*)/\"\\1\"/' | xargs -n 1 -L 1 pip install"
  if [[ ${major} == "3" ]]; then
    su jenkins -c ". /envs/h2o_env_python${python_version}/bin/activate && grep -v \"^#\" docs-requirements.txt | sed -r 's/(.*)/\"\\1\"/' | xargs -n 1 -L 1 pip install"    
  fi
  su jenkins -c "mkdir -p ~/.config/matplotlib/ && echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc"
  echo "###### Show all packages installed for Python ${python_version} ######"
  su jenkins -c ". /envs/h2o_env_python${python_version}/bin/activate && pip freeze"
done

rm *-requirements*.txt
